public class Edge implements Comparable {

  float minX;
  int minY;
  int maxY;
  float m;
  boolean vertical=false;

  public Edge(int x0, int y0, int x1, int y1) {
    int maxX;
    if (y0<y1) {
      minY = y0;                
      minX = x0;
      maxY = y1;
      maxX = x1;
    }
    else {
      minY = y1;
      minX = x1;
      maxY = y0;
      maxX = x0;
    }    
    if(minX!=maxX) m = (float)(minY-maxY)/(minX-maxX);
    else vertical = true;
    //println("Created edge with slope : " + m);
  }

  public void update() {
    minX += (vertical)?0:1.0f/m;
    minY++;
  }

  public int compareTo(Object obj) {

    Edge e2 = (Edge) obj;

    if (this.minY < e2.minY) {
      return -1;
    }
    else if (this.minY  == e2.minY) {
      if (this.minX < e2.minX) {
        return -1;
      }
      else if (this.minX == e2.minX) {
        if(((this.m > 0)&&(e2.m<0))) return 1;
        if (this.m > e2.m || ((this.m < 0)&&(e2.m>0))) {
          return -1;
        }
        else if (this.m == e2.m) {
          return 0;
        }     
        else {
          return 1;
        }
      } 
      else {
        return 1;
      }
    }
    else {
      return 1;
    }
  }
  
  public int getMinX(){
    return (int)minX;
  }
  
}

